package com.quad.innovators.salesease.controller;

import com.quad.innovators.salesease.annotation.DataPermission;
import com.quad.innovators.salesease.annotation.FunctionPermission;
import com.quad.innovators.salesease.annotation.Log;
import com.quad.innovators.salesease.annotation.RepeatSubmit;
import com.quad.innovators.salesease.common.result.Result;
import com.quad.innovators.salesease.common.utils.SecurityUtils;
import com.quad.innovators.salesease.constants.SystemConstants;
import com.quad.innovators.salesease.enums.LogModuleEnum;
import com.quad.innovators.salesease.model.bo.RoleDataPermissionBO;
import com.quad.innovators.salesease.model.bo.RoleMenuBO;
import com.quad.innovators.salesease.model.common.Option;
import com.quad.innovators.salesease.model.form.SysMenuForm;
import com.quad.innovators.salesease.model.query.SysMenuQuery;
import com.quad.innovators.salesease.model.vo.RouteVO;
import com.quad.innovators.salesease.model.vo.SysMenuVO;
import com.quad.innovators.salesease.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@Tag(name = "04.菜单接口")
@RestController
@RequestMapping(SystemConstants.BACKEND_API +  "/menus")
@RequiredArgsConstructor
@Slf4j
public class SysMenuController {

    private final SysMenuService sysMenuService;

    @Operation(summary = "菜单列表")
    @GetMapping
    @Log( value = "菜单列表",module = LogModuleEnum.MENU)
    @DataPermission(permissionCode = "sys:menu:data-permission")
    public Result<List<SysMenuVO>> listMenus(SysMenuQuery queryParams) {
        List<SysMenuVO> menuList = sysMenuService.listMenus(queryParams);
        return Result.success(menuList);
    }

    @Operation(summary = "菜单下拉列表")
    @GetMapping("/options")
    @Parameters({
            @Parameter(name = "onlyParent", description = "是否只查询父级菜单")
    })
    @DataPermission(permissionCode = "sys:menu:data-permission")
    public Result<List<Option<String>>> listMenuOptions(@RequestParam(required = false, defaultValue = "false") boolean onlyParent) {
        List<Option<String>> menus = sysMenuService.listMenuOptions(onlyParent);
        return Result.success(menus);
    }

    @Operation(summary = "菜单路由列表")
    @GetMapping("/routes")
    @DataPermission(permissionCode = "sys:menu:data-permission", userAlias = "t1")
    public Result<List<RouteVO>> listRoutes() {
        Set<String> roles = SecurityUtils.getRoles();
        List<RouteVO> routeList = sysMenuService.listRoutes(roles);
        return Result.success(routeList);
    }

    @Operation(summary = "菜单角色列表")
    @GetMapping("/role/options")
    @DataPermission(permissionCode = "sys:menu:data-permission")
    public Result<List<RoleMenuBO>> listRoleMenuOptions() {
        List<RoleMenuBO> routeList = sysMenuService.listMenuOptions();
        return Result.success(routeList);
    }

    @Operation(summary = "菜单数据权限角色列表")
    @GetMapping("/role/data-permission/options")
    @DataPermission(permissionCode = "sys:menu:data-permission")
    public Result<List<RoleDataPermissionBO>> listRoleDataPermissionOptions() {
        List<RoleDataPermissionBO> routeList = sysMenuService.listRoleDataPermissionOptions();
        return Result.success(routeList);
    }

    @Operation(summary = "菜单表单数据")
    @GetMapping("/{code}/form")
    @Parameters({
            @Parameter(name = "code", description = "菜单ID")
    })
    @DataPermission(permissionCode = "sys:menu:data-permission")
    public Result<SysMenuForm> getMenuForm( @PathVariable("code") String code) {
        SysMenuForm menu = sysMenuService.getMenuForm(code);
        return Result.success(menu);
    }

    @Operation(summary = "新增菜单")
    @PostMapping
    @FunctionPermission(permissionCode = "sys:menu:add")
    @RepeatSubmit
    public Result<?> addMenu(@Valid @RequestBody SysMenuForm menuForm) {
        boolean result = sysMenuService.saveMenu(menuForm);
        return Result.judge(result);
    }

    @Operation(summary = "修改菜单")
    @PutMapping(value = "/{code}")
    @FunctionPermission(permissionCode = "sys:menu:edit")
    @Parameters({
            @Parameter(name = "code", description = "菜单ID",required = true)
    })
    public Result<?> updateMenu(@PathVariable("code") String code, @RequestBody SysMenuForm menuForm) {
        boolean result = sysMenuService.updateMenu(code, menuForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除菜单")
    @DeleteMapping("/{code}")
    @FunctionPermission(permissionCode = "sys:menu:delete")
    @Parameters({
            @Parameter(name = "code", description = "菜单ID")
    })
    public Result<?> deleteMenu(@PathVariable("code") String code) {
        boolean result = sysMenuService.deleteMenu(code);
        return Result.judge(result);
    }

    @Operation(summary = "修改菜单显示状态")
    @PatchMapping("/{menuCode}")
    @Parameters({
            @Parameter(name = "menuCode", description = "菜单ID"),
            @Parameter(name = "visible", description = "显示状态(1:显示;0:隐藏)")
    })
    public Result<?> updateMenuVisible(@PathVariable("menuCode") String menuCode,@RequestParam("visible") Integer visible) {
        boolean result = sysMenuService.updateMenuVisible(menuCode, visible);
        return Result.judge(result);
    }

}

